﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using log4net;
using CashFlow.Db.Entity;
using SysAdmin._00_Def;
using SysAdmin._03_Service._00_Db;
using SysAdmin._04_Form._00_Common;


namespace SysAdmin._04_Form._01_Shop
{
    public partial class AddShopForm : Form
    {
        private static ILog _logger = LogManager.GetLogger(typeof(AddShopForm));
        private UserInfoEntity ShopManagerUserInfo { get; set; }
        private IList<UserInfoEntity> ShopUserInfoList { get; set; }
        public int ParentShopManagerUserId { get; set; }

        public AddShopForm()
        {
            InitializeComponent();
        }

        private void AddShopForm_Load(object sender, EventArgs e)
        {
            label4.Text = "未选择";
        }

        private void AddShopForm_Shown(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            var form = new SelectShopUserForm();
            form.ParentShopManagerUserId = ParentShopManagerUserId;
            var dr = form.ShowDialog();
            if (dr == DialogResult.OK)
            {
                ShopUserInfoList = form.SelectedUserInfoList;
                ShopManagerUserInfo = ShopUserInfoList.ElementAt(0);
                var ls = ShopUserInfoList.Select(x => x.Name).ToList();
                listBox1.BeginUpdate();
                listBox1.Items.Clear();
                listBox1.Items.AddRange(ls.ToArray());
                listBox1.EndUpdate();
                label4.Text = ShopManagerUserInfo.Name;
                label5.Text = string.Format("会员人数: {0}", ShopUserInfoList.Count);
            }
        }

        private bool ValidateForm()
        {
            if (ShopManagerUserInfo == null)
            {
                UICommonTool.ShowAlertDialog("未选择新店铺店长！");
                return false;
            }

            if (string.IsNullOrEmpty(textBox1.Text.Trim()))
            {
                UICommonTool.ShowAlertDialog("店铺名换不能为空！");
                return false;
            }

            if (listBox1.Items.Count == 0)
            {
                UICommonTool.ShowAlertDialog("未选择新店铺会员！");
                return false;
            }

            return true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (ValidateForm())
                {
                    if (UICommonTool.ShowConfirmDialog("确定要开新店铺吗？"))
                    {
                        if (ShopService.AddShop(textBox1.Text.Trim(), ShopManagerUserInfo, ShopUserInfoList))
                        {
                            UICommonTool.ShowInfoDialog("开新店铺成功！");
                            DialogResult = DialogResult.OK;
                            Close();
                        }
                        else
                        {
                            UICommonTool.ShowAlertDialog("开新店铺失败！");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                UICommonTool.ShowAlertDialog(ex.Message);
            }
        }
    }
}
